﻿// Decompiled with JetBrains decompiler
// Type: MCM.LightInject.DynamicMethod
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;

#nullable disable
namespace MCM.LightInject
{
  [ExcludeFromCodeCoverage]
  internal class DynamicMethod
  {
    private readonly Type returnType;
    private readonly Type[] parameterTypes;
    private readonly ParameterExpression[] parameters;
    private readonly ILGenerator generator;

    public DynamicMethod(Type returnType, Type[] parameterTypes)
    {
      this.returnType = returnType;
      this.parameterTypes = parameterTypes;
      // ISSUE: reference to a compiler-generated field
      // ISSUE: reference to a compiler-generated field
      this.parameters = ((IEnumerable<Type>) parameterTypes).Select<Type, ParameterExpression>(DynamicMethod.\u003C\u003EO.\u003C0\u003E__Parameter ?? (DynamicMethod.\u003C\u003EO.\u003C0\u003E__Parameter = new Func<Type, ParameterExpression>(Expression.Parameter))).ToArray<ParameterExpression>();
      this.generator = new ILGenerator(this.parameters);
    }

    public Delegate CreateDelegate(Type delegateType)
    {
      return Expression.Lambda(delegateType, this.generator.CurrentExpression, this.parameters).Compile();
    }

    public Delegate CreateDelegate(Type delegateType, object target)
    {
      InvocationExpression body = Expression.Invoke((Expression) Expression.Lambda(Expression.GetDelegateType(((IEnumerable<Type>) this.parameterTypes).Concat<Type>((IEnumerable<Type>) new Type[1]
      {
        this.returnType
      }).ToArray<Type>()), this.generator.CurrentExpression, true, this.parameters), ((IEnumerable<Expression>) new Expression[1]
      {
        (Expression) Expression.Constant(target)
      }).Concat<Expression>(this.parameters.Cast<Expression>().Skip<Expression>(1)).ToArray<Expression>());
      return Expression.Lambda(delegateType, (Expression) body, ((IEnumerable<ParameterExpression>) this.parameters).Skip<ParameterExpression>(1)).Compile();
    }

    public ILGenerator GetILGenerator() => this.generator;
  }
}
